package com.bysj.money_diary;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Chazhangben extends Fragment implements OnClickListener,OnItemClickListener{
	private View view;
	private RelativeLayout back_rl,next_rl,zhichu_rl,shouru_rl;
	private LinearLayout huizong_ll,list_ll;
	private ListView listView;
	private TextView time_text,huizong_text,zhichu_text,shouru_text,shengyu_text;
	private int year,month;
	private String nowTime;
	private int flag=0;
	public static final int shouru_flag=4010;
	public static final int zhichu_flag=4020;
	public static MessageHandler mh;
	private ChazhangbenAdapter adapter;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_chazhangben, container, false);
		mh = new MessageHandler();
		adapter = new ChazhangbenAdapter(this.getActivity());
		init();
		return view;
	}

	private void init() {
		back_rl = (RelativeLayout)view.findViewById(R.id.chazhangben_back_rl);
		back_rl.setOnClickListener(this);
		next_rl = (RelativeLayout)view.findViewById(R.id.chazhangben_next_rl);
		next_rl.setOnClickListener(this);
		zhichu_rl = (RelativeLayout) view.findViewById(R.id.chazhangben_zhichu_rl);
		zhichu_rl.setOnClickListener(this);
		shouru_rl = (RelativeLayout) view.findViewById(R.id.chazhangben_shouru_rl);
		shouru_rl.setOnClickListener(this);
		huizong_ll = (LinearLayout)view.findViewById(R.id.chazhangben_huizong_ll);
		huizong_ll.setVisibility(View.GONE);
		list_ll = (LinearLayout)view.findViewById(R.id.chazhangben_list_ll);
		time_text = (TextView)view.findViewById(R.id.chazhangben_time_text);
		huizong_text = (TextView)view.findViewById(R.id.chazhangben_huizong);
		huizong_text.setOnClickListener(this);
		zhichu_text = (TextView)view.findViewById(R.id.chazhangben_benyuezhichu_text);
		shouru_text = (TextView)view.findViewById(R.id.chazhangben_benyueshouru_text);
		shengyu_text = (TextView)view.findViewById(R.id.chazhangben_benyueshengyu_text);
		
		year = GetTime.getYear();
		month = GetTime.getMonth();
		if(month<10){
			nowTime = year+"年"+month+"月";
		}else{
			nowTime = year+"年"+month+"月";	
		}
		time_text.setText(nowTime);
		listView  = (ListView) view.findViewById(R.id.chazhangben_list);
		flag = zhichu_flag;
		getHuiZong();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		listView.setLayoutAnimation(DongHua3d.listDongHua());
	}

	private void getHuiZong() {
		double count[] = adapter.getList(year, month, 0, flag);
		zhichu_text.setText(count[0]+"");
		shouru_text.setText(count[1]+"");
		shengyu_text.setText((count[1]-count[0])+"");
		adapter.notifyDataSetChanged();
		
	}
	
	private void yanChiShow(final View v) {
		if(flag == zhichu_flag){
			zhichu_rl.setBackgroundResource(R.drawable.jiyibi_bt_bg_s);
			shouru_rl.setBackgroundResource(R.drawable.blank);
		}else if(flag == shouru_flag){
			shouru_rl.setBackgroundResource(R.drawable.jiyibi_bt_bg_s);
			zhichu_rl.setBackgroundResource(R.drawable.blank);
		}
    	new Thread(){
    		public void run(){
    			try {
    				mh.post(new Runnable(){
        				public void run(){
        					DongHua3d.applyRotation(v, 0, 180, 0);
        				 }
        				});
					sleep(400);
					mh.post(new Runnable(){
        				public void run(){
        					huiZongThread();
	    					DongHua3d.applyRotation(v, 0, 180, 1);
        				 }
    				});
    			} catch (InterruptedException e) {
					e.printStackTrace();
				}
    		}
    	}.start();
	}
	
	public void huiZongThread(){
    	new Thread(){
    		public void run(){
    			mh.post(new Runnable(){
    				public void run(){
    					getHuiZong();
    				 }
				});
    		}
    	}.start();
    }

	private void nextMonth(int flag2) {
		month++;
		if(month>12){
			month=1;
			year++;
		}
		if(month<10){
			nowTime = year+"年"+month+"月";
		}else{
			nowTime = year+"年"+month+"月";	
		}
		time_text.setText(nowTime);
		yanChiShow(list_ll);
	}

	private void backMonth(int flag2) {
		month--;
		if(month<1){
			month=12;
			year--;
		}
		if(month<10){
			nowTime = year+"年"+month+"月";
		}else{
			nowTime = year+"年"+month+"月";	
		}
		time_text.setText(nowTime);	
		yanChiShow(list_ll);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(this.getActivity(),MainActivity.class);
		if(flag == zhichu_flag){
			Zhichu zc = (Zhichu)view.getTag();
			intent.putExtra("update", true);
			intent.putExtra("type", Jiyibi.zhichu_flag);
			intent.putExtra("id", zc.getZc_Id());
			startActivity(intent);
		}else if(flag == shouru_flag){
			Shouru sr = (Shouru)view.getTag();
			intent.putExtra("update", true);
			intent.putExtra("type", Jiyibi.shouru_flag);
			intent.putExtra("id", sr.getSr_Id());
			startActivity(intent);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chazhangben_back_rl:
			backMonth(flag);
			break;
		case R.id.chazhangben_next_rl:
			nextMonth(flag);
			break;
		case R.id.chazhangben_zhichu_rl:
			flag = zhichu_flag;
			yanChiShow(list_ll);
			break;
		case R.id.chazhangben_shouru_rl:
			flag  =shouru_flag;
			yanChiShow(list_ll);
			break;
		case R.id.chazhangben_huizong :
			if(huizong_ll.isShown()){
				huizong_text.setBackgroundResource(R.drawable.border_item1);
				DongHuaYanChi.dongHuaEnd(huizong_ll, Chazhangben.this.getActivity(), mh, R.anim.menu_down, 300);
			}else{
				huizong_text.setBackgroundResource(R.drawable.main_button_background);
				huizong_ll.setAnimation(AnimationUtils.loadAnimation(Chazhangben.this.getActivity(),R.anim.menu_up));
				huizong_ll.setVisibility(View.VISIBLE);
			}
			break;
		}
	}
	
	@Override  
	public void setUserVisibleHint(boolean isVisibleToUser) {  
		super.setUserVisibleHint(isVisibleToUser);  
	  	if (isVisibleToUser) {  
	  		//相当于Fragment的onResume  
	  		getHuiZong();
	  		this.getActivity().overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
			super.onResume();
	 	} else {  
	 		//相当于Fragment的onPause  
	 	}  
	}
	
	public boolean onKeyDown(int kCode, KeyEvent kEvent) {
		switch (kCode) {
		case KeyEvent.KEYCODE_BACK: {
			this.getActivity().finish();
			return false;
		 }
		}
		return super.getActivity().onKeyDown(kCode, kEvent);
	}

	public void showMsg(String msg) {
		Toast.makeText(this.getActivity(), msg, Toast.LENGTH_SHORT).show();
	}

	@SuppressLint("HandlerLeak")
	public class MessageHandler extends Handler {
		public void handleMessage(Message msg) {
			
		}
	}
	
}
